//
//  HMEmoticonTools.swift
//  Weibo
//
//  Created by 李学林 on 15/12/15.
//  Copyright © 2015年 tete. All rights reserved.
//

import UIKit

//*********************************炒
// 一页表情有多少行
let HMEmoticonsRowOfPage = 3
// 一页表情有多少列
let HMEmoticonsColOfPage = 7
/// 一页显示多少个表情数据
let HMEmoticonsNumOfPage = HMEmoticonsRowOfPage * HMEmoticonsColOfPage - 1

class HMEmoticonTools: NSObject {
    
    // 提供全局访问点
    static let shareTools: HMEmoticonTools =  HMEmoticonTools()
    
    // 表情数据的 bundle 文件
    lazy var emoticonBundle: NSBundle = {
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        return NSBundle(path: path)!
    }()
    
    // 最近表情
    lazy var recentEmoticons: [HMEmoticon] = [HMEmoticon]()
    
    // 读取默认表情数据
    lazy var defaultEmoticons: [HMEmoticon] = {
        // default/info.plist
        return self.emoticonsWithPath("default/info.plist")
    }()
    
    /// emoji表情
    lazy var emojiEmoticons: [HMEmoticon] = {
        return self.emoticonsWithPath("emoji/info.plist")
    }()
    
    /// 浪小花表情
    lazy var lxhEmoticons: [HMEmoticon] = {
        return self.emoticonsWithPath("lxh/info.plist")
    }()
    
    /// 供表情键盘的 collectionView 使用的数据
    lazy var allEmoticons: [[[HMEmoticon]]] = {
        return [
            self.sectionWithEmoticons(self.recentEmoticons),
            self.sectionWithEmoticons(self.defaultEmoticons),
            self.sectionWithEmoticons(self.emojiEmoticons),
            self.sectionWithEmoticons(self.lxhEmoticons)
        ]
    }()
    
    
    /// 比如: Emoji表情为80个 --> 分成 4组,每一组20个 --> [[HMEmoticon]]
    ///
    /// - parameter emoticons: <#emoticons description#>
    ///
    /// - returns: <#return value description#>
    private func sectionWithEmoticons(emoticons: [HMEmoticon]) -> [[HMEmoticon]] {
        
        // 80 / 20 = 4
        // 1. 取到当前表情一共有多少页
        // 95
        let pageCount = (emoticons.count - 1) / HMEmoticonsNumOfPage  + 1
        // 定义一个返回的数组
        var result = [[HMEmoticon]]()
        // 2. 遍历截取每一页显示的数据
        for i in 0..<pageCount {
            
            // 计算截取范围
            let location = i * HMEmoticonsNumOfPage
            var length = HMEmoticonsNumOfPage
            // 如果开始截取位置加上截取长度大于数组的个数,那么就代表越界
            if location + length > emoticons.count {
                // 调整 length --> 数组的长度 - 开始截取的位置
                length = emoticons.count - location
            }
            
            let range = NSMakeRange(location, length)
            
            // 截取子数组
            let subArray = (emoticons as NSArray).subarrayWithRange(range) as! [HMEmoticon]
            // 添加到返回数组内
            result.append(subArray)
        }
        return result
    }
    
    
    /// 读取表情数据
    ///
    /// - parameter path: 对应表情 bundle 里面的 路径 (比如: lxh/info.plist)
    ///
    /// - returns: 对应表情数组
    private func emoticonsWithPath(path: String) -> [HMEmoticon]{
        let file = self.emoticonBundle.pathForResource(path, ofType: nil)!
        // 读取 info.plist 文件里面的内容
        let array = NSArray(contentsOfFile: file)!
        var result = [HMEmoticon]()
        for value in array {
            // 判断是否是字典
            if let dict = value as? [String: AnyObject] {
                // 读取 info.plist 文件路径是 file
                // 因为图片是与 info.plist 文件放在一起的,所以其前面路径一样
                // 所以就把 `file` 后面的文件名删掉
                let emoticon = HMEmoticon(dict: dict)
                emoticon.path = (file as NSString).stringByDeletingLastPathComponent
                result.append(emoticon)
            }
        }
        return result
    }
    
}
